package cn.iflags.Mall.service.impl;

import cn.iflags.Mall.common.ServerResponse;
import cn.iflags.Mall.dao.PasteMapper;
import cn.iflags.Mall.pojo.Paste;
import cn.iflags.Mall.service.IPasteService;
import cn.iflags.Mall.util.DataUtil;
import cn.iflags.Mall.util.FileUtil;
import cn.iflags.Mall.util.JsonUtil;
import cn.iflags.Mall.util.PathUtil;
import cn.iflags.Mall.vo.PasteVo;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * 描述:代码贴实现服务
 * @author Vincent Vic
 * create 2020-03-15 9:55
 */

@Service("iPasteService")
public class IPasteServiceImpl implements IPasteService {
    private static Logger log = LoggerFactory.getLogger(IPasteServiceImpl.class);


    @Autowired
    private PasteMapper pasteMapper;

    @Override
    public ServerResponse uploadPaste(String ip,Paste paste, String codeData) {

        paste.setIp(ip);
        //校验数据信息
        //标题不存在
        if (paste.getTitleName() == null || paste.getTitleName().equals(" ")){
            return ServerResponse.createByErrorMessage("代码标题不能为空");
        } else if (paste.getTitleName().length() > 35){
            return ServerResponse.createByErrorMessage("代码标题太长");
        }
        //数据不存在
        if(codeData == null || codeData.equals(" ")){
            return ServerResponse.createByErrorMessage("代码没有粘贴");
        } else if(codeData.length()> 150000){
            return ServerResponse.createByErrorMessage("本站不支持太长的代码");
        }
        //以防数据库不够用
        if (paste.getDetail().length()>=400){
            paste.setDetail(paste.getDetail().substring(0,400));
        }
        //默认style
        if (paste.getStyle().equals("默认主题(Monokai-Sublime)")){
            paste.setStyle("Monokai-Sublime");
        }
        //默认语言
        if (paste.getSyntax().equals("默认语言(none)")){
            paste.setStyle("txt");
        }

        //生成key，存在重新生成
        String keyUUID= null;
        int isKey =0 ;
        do{
            keyUUID= DataUtil.generateShortUuid();
            isKey = pasteMapper.checkKey(keyUUID);
        }while (isKey != 0);
        //设置paste加入数据库
        paste.setPaste_key(keyUUID);

        //加密code文件

        //创建code文件
        try {
            Boolean fileStatus = FileUtil.createFile(PathUtil.getUploadResources()+ File.separator+"paste"+File.separator,keyUUID,codeData);
            if (fileStatus == false){
                return ServerResponse.createByErrorMessage("提交失败，联系管理员，错误代码：0xff0IP1");
            }
        } catch (FileNotFoundException e) {
            log.error("文件创建失败"+e);
            e.printStackTrace();
        }

        pasteMapper.insert(paste);

        return ServerResponse.createBySuccessMessage(keyUUID);
    }

    @Override
    public ServerResponse<PasteVo> getPaste(String key) {
        Paste paste = pasteMapper.selectByPasteKey(key);
        String style = null;
        //得到样式表json
        JSONObject jsonObject = JsonUtil.LoadJsonObject(PathUtil.getUploadResources()+"json"+File.separator+"PasteCodeBlockStyle.json");
        if(jsonObject == null){
            System.out.println("json error");
            style = "vs.css";
            log.error("样式PasteCodeBlockStyle.json 读取错误");
        }else {
            style = (String) jsonObject.get(paste.getStyle());
            System.out.println(style);
            if (style == null) {
                style = "vs.css";
                log.error("样式" + style + ".css 读取错误");
            }
        }

        String code = FileUtil.readFile(PathUtil.getUploadResources()+ File.separator+"paste"+File.separator+paste.getPaste_key());
        if (code == null){
            return ServerResponse.createByErrorMessage("系统错误");
        }
        //加入PasteVo
        PasteVo pasteVo = new PasteVo(paste,code,style);
        return ServerResponse.createBySuccess("成功",pasteVo);
    }
}